# -*- coding: utf-8 –*-
import datetime

from mongoengine import (
    StringField, IntField, DateTimeField,
    Document, ReferenceField, BooleanField,
)

from app.constants import DEVICE_TYPES


class DeviceId(Document):

    """
    设备访问信息
    """

    meta = {
        'collection': 'vacation_deviceid',
    }

    device_id = StringField(max_length=100)
    device = IntField(default=DEVICE_TYPES.IOS)
    name = StringField()
    tel =StringField()
    consultation_date = DateTimeField(default=datetime.datetime.utcnow)
    source = IntField()      # for Android
    last_modified = DateTimeField(default=datetime.datetime.utcnow)
    date_added = DateTimeField(default=datetime.datetime.utcnow)

    def get_info(self):
        return {
            'device_id': self.device_id,
            'source': self.source,
        }

    def __repr__(self):
        return '<DeviceId: %s>' % self.device_id


class Statistics(Document):

    meta = {
        'collection': 'vacation_statistics',
    }

    device = ReferenceField(DeviceId)
    category = IntField()
    date_added = DateTimeField(default=datetime.datetime.utcnow)


class UserDeviceToken(Document):
    """
    Both android and ios will share this model
    """
    meta = {
        'collection': 'btmisc_userdevicetoken',
        'indexes': [
            {'fields': ('user_id', 'device', 'token'), 'unique': True}
        ],
    }

    user_id = IntField()
    device = IntField()
    token = StringField()
    is_active =BooleanField(default=True)
    date_added = DateTimeField(default=datetime.datetime.utcnow)

    def __str__(self):
        return '<UserDeviceToken: %s(%s): %s>' % \
               (self.user_id, self.device, self.token)


class DeviceToken(Document):
    """
    记录每一个安装app的人的device_token
    """
    meta = {
        'collection': 'btmisc_devicetoken',
    }

    device = IntField(default=DEVICE_TYPES.IOS)
    device_token = StringField(unique=True)
    is_active = BooleanField(null=True)
    last_registed = DateTimeField(default=datetime.datetime.utcnow, null=True)
    date_added = DateTimeField(default=datetime.datetime.utcnow)
    #app版本号
    breadtrip_version = StringField(null=True)

    def __str__(self):
        return '<DeviceToken: %s>' % self.device_token

